<div class="red" style="line-height: 30px;">
    【操作提示】：排序为-1表示不启用此游戏！
</div>

<table class="am-table am-table-bordered am-table-radius am-table-striped game-setting">
    <thead>
        <tr>
            <th>游戏名称</th>
            <th>排序</th>
            <th>开奖间隔(秒)</th>
            <th>封盘时间(秒)</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

<div class="button_line">
    <button class="save am-btn-xl am-btn-primary admin_button">保存</button>
</div>

<script>
    // 填入游戏
    var games = get_game_list();
    var table = $(".game-setting tbody");
    var fps = {};
    for (var i in games) {
        var tr = $("<tr value='" + games[i] + "'></tr>");
        tr.append("<td>" + get_game_display_name(games[i]) + "</td>");
        tr.append("<td class='order'>" + "<input type='text' class='order empty_input' value='1'/></td>");
        if (games[i].startsWith("zy")) {
            tr.append("<td class='internal'>" + "<input type='number' class='internal empty_input' value=''/></td>");
        }
        else {
            tr.append("<td></td>");
        }
        tr.append('<td class="fp"><input type="number" class="fp empty_input" value="60"/></td>');
        table.append(tr);
    }
    $("input[type='checkbox']").uCheck();

    // 各游戏的排序
    Classify.queryExt("games", "order", true, {}, function(r) {
        var trs = table.find("tr");
        for (var i = 0; i < trs.length; i++) {
            var game = $(trs[i]).attr("value");
            if (!game) continue;
            var o = $(trs[i]).find(".order input");
            o.val(r[game] || 0);
        }
    });

    // 各彩种的开奖间隔
    Classify.queryGroup("lottery.type.end", function(list) {
        var m = Util.toMapping(list, "key");
        var trs = table.find("tr");
        for (var i = 0; i < trs.length; i++) {
            var game = $(trs[i]).attr("value");
            if (!game) continue;
            var o = $(trs[i]).find("input.fp");
            if (m[game]) o.val(m[game].value);
        }
        fps = m;
    });
    Classify.queryGroup("lottery.type.interval", function(list) {
        var m = Util.toMapping(list, "key");
        var trs = table.find("tr");
        for (var i = 0; i < trs.length; i++) {
            var game = $(trs[i]).attr("value");
            if (!game) continue;
            var o = $(trs[i]).find("input.internal");
            if (m[game]) o.val(m[game].value);
        }
        fps = m;
    });

    $(".save").click(function() {
        // 保存所有的游戏开奖时间
        var trs = table.find("tr");
        var orders = {};
        for (var i = 0; i < trs.length; i++) {
            var game = $(trs[i]).attr("value");
            if (!game) continue;
            var o = $(trs[i]).find("input.fp");
            var v = o.val();
            if (v <= 0) {
                alert2("封盘时间应该大于0秒！", true);
                return;
            }
            if (!fps[game] || v != fps[game].value)
                Classify.save("lottery.type.end", game, v + "");
            if (!fps[game]) {
                fps[game] = {
                    value: v + "",
                };
            }
            else fps[game].value = v + "";

            var o = $(trs[i]).find("input.internal");
            if (o.length > 0) {
                var v = o.val();
                if (v <= 0) {
                    alert2("开奖间隔应该大于0秒！", true);
                    return;
                }
                Classify.save("lottery.type.interval", game, v + "");
            }
            
            o = $(trs[i]).find(".order input");
            orders[game] = o.val();
        }

        startLoading();
        Classify.saveExt("games", "order", orders, function() {
            stopLoading();
            alert2("保存成功！");
        });
    });
</script>